package com.yj.lottery_system.common.pojo;

import com.yj.lottery_system.common.errorcode.ErrorCode;
import com.yj.lottery_system.common.errorcode.GlobalErrorCodeConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

import java.io.Serializable;
@Data
@AllArgsConstructor         // 全参构造
@NoArgsConstructor          // 无参构造（Jackson 反序列化需要）
public class CommonResult <T> implements Serializable {
    //返回错误码
    private Integer code;
    //返回错误信息
    private String msg;
    //返回正常数据
    private T data;
    public static <T> CommonResult<T> success( T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        commonResult.msg = GlobalErrorCodeConstants.SUCCESS.getMsg();
        commonResult.data = data;
        return commonResult;

    }
    public static <T> CommonResult<T> error( Integer code, String msg) {
        //断言
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"code: "+code+" 不是一个异常的错误码");

        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = code;
        commonResult.msg = msg;
        commonResult.data = null;

        return commonResult;

    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        //断言
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(errorCode.getCode()),"code: "+errorCode.getCode()+" 不是一个异常的错误码");

        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.code = errorCode.getCode();
        commonResult.msg = errorCode.getMsg();

        return commonResult;

    }
}
